package su.msk.jet.team01.chatik.client.ui.swing;

import javax.swing.*;
import java.awt.*;

@SuppressWarnings("serial")
class MessageBox extends JPanel {
	private static final int DEFAULT_COLUMS_NUMBER = 80;
	private static final int DEFAULT_ROWS_NUMBER = 20;

	private final JTextArea textBox;

	MessageBox() {
		final int numb = 5;
		final JScrollPane scrollPane;
		textBox = new JTextArea(DEFAULT_ROWS_NUMBER, DEFAULT_COLUMS_NUMBER);
		textBox.setEditable(false);
		textBox.setLineWrap(true);
		textBox.setWrapStyleWord(true);
		this.add(textBox);

		scrollPane = new JScrollPane(textBox);
		this.add(scrollPane);

		setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
		setLayout(new GridLayout(0, 1, 0, numb));
	}

	void print(final String str) {
		textBox.append(str);

		scroll();
	}

	void println(final String str) {
		textBox.append(str);
		textBox.append("\n");

		scroll();
	}

	private void scroll() {
		textBox.setCaretPosition(textBox.getDocument().getLength());
	}
}